package com.kilman.logger.log;

import java.util.ArrayList;
import java.util.List;

import com.kilman.logger.log.control.LogLine;
import com.kilman.logger.log.control.LogLinesController;


public class ActiveLog {

    public enum ActiveLogState {
        NEW, RUNNING, STOPPED
    }

    private LogLinesController logLinesController;
    private Thread rawActiveLogThread = null;
    private RawActiveLog rawActiveLog = null;
    private List<LogLine> lines = null;
    private ActiveLogState state = ActiveLogState.NEW;
    
    public ActiveLog(String path, String host, String username, String password, 
            LogLinesController logLinesController) {
        this.logLinesController = logLinesController;
        rawActiveLog = new RawActiveLog(path, host, username, password);
        rawActiveLogThread = new Thread(rawActiveLog);
        lines = new ArrayList<LogLine>();
    }
    
    public void start() {
        rawActiveLogThread.start();
        state = ActiveLogState.RUNNING;
    }
    
    public void stop() {
        rawActiveLog.endExecution();
        state = ActiveLogState.STOPPED;
    }
    
    public void updateLines() {
        List<String> rawLines = rawActiveLog.getOutput();
        synchronized (rawLines) {
            for (int i = lines.size(); i < rawLines.size(); i++) {
                String currentRawLine = rawLines.get(i);
                LogLine currentLine = logLinesController.parseLine(currentRawLine, i+1);
                lines.add(currentLine);
            }
        }
    }
    
    public List<LogLine> getLines() {
        return lines;
    }
    
    public RawActiveLog getRawActiveLog() {
        return rawActiveLog;
    }

    public ActiveLogState getState() {
        return state;
    }

    public void setLogLinesController(LogLinesController logLinesController) {
        this.logLinesController = logLinesController;
    }

    public LogLinesController getLogLinesController() {
        return logLinesController;
    }
  
}
